﻿myApp.controller('BlogController', function ($window, $compile, $route, $rootScope, $scope, $http, $timeout, $location, DUMMY_DATA, $routeParams) {

    $scope.isAdmin = $rootScope.isAdmin;
    $scope.isSuccess = false;

    //$.get($route.current.templateUrl, function (data) {
    //    alert("DATA!!: " + data);
    //    $scope.html = data;
        
    //});

    //alert("RUNNING...");

    $scope.isAdmin = $rootScope.isAdmin;

    $http.get('/api/Blog/GetBlog?Url=' + $routeParams.name)
        .success(function (data) {
            alert("DATA: " + JSON.stringify(data));

            var data = data[0];
            $scope.html = data.contents.replace(/'/g, "\"");
            $scope.title = data.title;
            $scope.$emit('newPageLoaded', {
                'title': data.metatitle,
                'description': data.metadescription,
                'keywords': data.keywords
            });
        })
        .error(function (err) {
            $window.location.href = "/";
        });

    //$http.get("https://jsonblob.com/api/jsonBlob/59dabf1a-05a5-11e7-a0ba-3d29ef7ce3f8")
    //    .success(function (data) {
    //       // alert(JSON.stringify(data));
    //        if (data.url == $routeParams.name) {
    //            $scope.html = data.contents.replace(/'/g, "\"");
    //            $scope.title = data.title;
    //            $scope.$emit('newPageLoaded', {
    //                'title': data.metatitle,
    //                'description': data.metadescription,
    //                'keywords': data.keywords
    //            });
    //        } else {
    //            //alert("Blog not found!")
    //            $window.location.href = "/";
    //        }
    //    })
    //    .error(function (err) {
    //        alert("ERR: " + JSON.stringify(err));
    //    });


    $scope.deleteBlog = function () {

        $http.delete('/api/Blog/DeleteBlog?url=' + $routeParams.name)
           .success(function (data) {
               if (data[0].C_ResultType == "Success") {
                   $scope.isSuccess = true;
                   //alert("Blog had been removed successfully!");

                   $timeout(function() {
                       $location.path('/');
                   }, 5000);

               } else {
                   alert("Ops! There is something wrong. Please try again later.")
               }
           })
       .error(function (err) {
           alert("Ops! There is something wrong with server. Please try again later.")
       });

    }

    $scope.editBlog = function () {
        alert("EDIT");

        alert("LOCATION: " + $location.path());
        $location.path("/blog/update" + $location.path());

    }

    // Call API (Parameter: routeParams)
    // Retrieve Title
    // Retrieve Routes

    // Retrieve Meta Title
    // Retrieve Meta Description


    // Retrieve Data Content

    //$scope.title = "New Blog Post";
    

    //$route.current.templateUrl = '/app/views/blogs/' + $routeParams.name + ".html";
    //$http.get($route.current.templateUrl).success(function (data) {
    //    if (data.substring(0, 15) == "<!DOCTYPE html>") {
    //        alert("Error!")
    //        $window.location.href = "/";
           
    //    } else {
    //        $scope.html = data;
    //    }
    //});



});